/*****************************************************
This program was produced by the
CodeWizardAVR V1.25.8a Pro

Project : Termometr
Version : 2.0.5 (  +   ..100 . +    ) 
Date    : 02.02.2008 ( )
Author  : Danil                          
Company : home                            
Comments:        ds18b20.lib
               int    float

Chip type           : ATtiny2313
Clock frequency     : 4,000000 MHz
Memory model        : Tiny
External SRAM size  : 0
Data Stack size     : 32
*****************************************************/
//
//   DS18B20 - (PORTA bit 0)
//
//   -  (PORTD bit 0, 1) -     
//
//    (..)
//                    - (PORTB)   "-3 "
//                    - (PORTD) digit1 - bit 3, digit2 - bit 4, digit3 - bit 5, digit4 - bit 6                  
//
//   -  (PORTD bit 2)

//  -          ,    IBM PC XT   Intel 8088 
#include <tiny2313.h>

// 1 Wire Bus functions (PORTA bit 0)
#asm
   .equ __w1_port=0x1b
   .equ __w1_bit=0
#endasm
//#include <1wire.h>

// DS18b20 Temperature Sensor functions
#include <ds18b20.h>  
#include <delay.h>         

flash unsigned char digits[] = {
        0xD7, // 0
        0x14, // 1
        0xCD, // 2
        0x5D, // 3
        0x1E, // 4
        0x5B, // 5
        0xDB, // 6
        0x15, // 7
        0xDF, // 8
        0x5F, // 9
        0x08, // 10 -  
        0x0F, // 11 -   
        0x00, // 12 - 
        0x8B, // 13 - F
        0x98, // 14 - n
        0xFC, // 15 - d.
        0xCB, // 16 - E
        0x88, // 17 - r
        0xA8  // 18 - r.
};                  

#define MINUS  10
#define GRADUS 11
#define PROBEL 12 

flash unsigned char cursor[]={0b11110111, 0b11101111, 0b11011111, 0b10111111}; // digit1  - PORTD.3, digit2 - PORTD.4, digit3 - PORTD.5, digit4 - PORTD.6
  
unsigned char digit_out[4], cur_dig, zpt_ON;
int temperature;          

/* maximum number of DS18B20 connected to the 1 Wire bus */
#define MAX_DEVICES 2

/* DS18B20 devices ROM code storage area */
unsigned char rom_code[MAX_DEVICES][9];

// Timer 1 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void) {
        PORTD|=0b01111000;                 //   (   "1" -  ..)
        PORTB=digits[digit_out[cur_dig]];       //   
        PORTD&=(cursor[cur_dig]);  //    (   "0" -  ..)
        
        if ((zpt_ON == 1) && (cur_dig == 1)) PORTB.5=1;  //     ( .  )

        cur_dig++; if (cur_dig>=4) cur_dig=0;
}

// Declare your global variables here
void hex_to_dec(void);

void main(void) { //================================================= main ========================================================
unsigned char devices;
// Declare your local variables here

// Crystal Oscillator division factor: 1
CLKPR=0x80;
CLKPR=0x00;

//     +     
DDRA=0x00;
PORTA=0x00;

//  B   (   0)
DDRB=0xFF;
PORTB=0x00;

//  D ( 6,5,4,3,2  ,  1,0  ,    ( 1,0) )
//   (   "1" -  ..,  6,5,4,3)
DDRD=0x7C;   // 01111100
PORTD=0xFF;  // 11111111

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0A output: Disconnected
// OC0B output: Disconnected
TCCR0A=0x00;
TCCR0B=0x00;
TCNT0=0x00;
OCR0A=0x00;
OCR0B=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 62,500 kHz
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
TCCR0A=0x00;
TCCR0B=0x03;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// Interrupt on any change on pins PCINT0-7: Off
GIMSK=0x00;
MCUCR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x02;

// Universal Serial Interface initialization
// Mode: Disabled
// Clock source: Register & Counter=no clk.
// USI Counter Overflow Interrupt: Off
USICR=0x00;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;

// 1 Wire Bus initialization
//w1_init();

// Global enable interrupts
#asm("sei")                     

 PORTD.2=0;
 devices=w1_search(0xf0,rom_code);

 //  1,5     Fnd '-  '
 digit_out[0]=13;    // F
 digit_out[1]=14;    // n
 digit_out[2]=15;    // d.
 digit_out[3]=devices; // 'devices'
 delay_ms(1500);
   
 // --------------------------------   -----------------------------------
 while (1) {
        // -----------------------------------------------------------
        //if (devices == 0) {
        //        digit_out[0]=16;    // E
        //        digit_out[1]=17;    // r
        //        digit_out[2]=18;    // r.
        //        digit_out[3]=devices; // 'devices'  
        //}
        
        // -----------------------------------------------------------
        if (devices >= 1) { //       
                temperature=ds18b20_temperature(&rom_code[0][0]); 
                if (temperature!=-9999) hex_to_dec();
        }
        

        devices=w1_search(0xf0,rom_code); //   ( )
 } // -----------------------------   (end) -----------------------------------

} // =============================================================== end main ==================================================

  

// -----------------------------------------------------------------------------------------------------------------------------
void hex_to_dec(void) {
        char i;
        unsigned char celie, drob;
        unsigned int temp, celie_tmp, drob_tmp;
 
        temp = (unsigned int) temperature;                      
        
        if (temperature>=0) {
                digit_out[0]=PROBEL;       //   ()  
        } 
        else {        
                temp = ( ~temp ) + 0x0001; //       . 
                digit_out[0]=MINUS;        //     (   ..)
             };
        digit_out[3]=GRADUS;

        celie_tmp = temp >> 4;     //   
        drob_tmp  = temp & 0x000F; //      1/16 
                
        celie = (unsigned char) celie_tmp;  //     
        drob  = (unsigned char) ((drob_tmp * 10) / 16); //      "1/16 "      0,1  
              
        if (celie >= 100) {
        
                zpt_ON=0; 
        
                i=0;
                while (celie >= 100) {     //  
                        celie=celie-100;
                        i++;
                };
                digit_out[0]=i;  
                        
                i=0;
                while (celie >= 10) {      // 
                        celie=celie-10;
                        i++;
                };
                digit_out[1]=i; 
        
                digit_out[2]=celie;       // 
                
        } else {
                if (digit_out[0] == MINUS) {
                        if (celie >= 10){
                                zpt_ON=0;
                                
                                i=0;
                                while (celie >= 10) {      // 
                                        celie=celie-10;
                                        i++;
                                };
                                digit_out[1]=i;       
                                
                                digit_out[2]=celie;        // 
                        } else {
                                        zpt_ON=1;                        
                                        digit_out[1]=celie;       // 
                                        digit_out[2]=drob;        //  
                               }
                } else {
                                zpt_ON=1;
                
                                i=0;
                                while (celie >= 10) {      // 
                                        celie=celie-10;
                                        i++;
                                };
                                if (i) digit_out[0]=i; //       
        
                                digit_out[1]=celie;       // 
                                digit_out[2]=drob;        //  
                        }
               }
        
        if (celie_tmp >= 100) {
                for (i=0;i<50;i++) {
                	PORTD.2=1;
                	delay_ms(4);
                	PORTD.2=0;
                	delay_ms(4);
                }
        }
}

//
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= The End -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
                                                                              